﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//todo 可以按照轴来计算放置对象的角度 
[ExecuteInEditMode]
public class BhvCircleLayout : MonoBehaviour
{

    public enum ModeEnum { AverageFill, FixedStep }

    public ModeEnum mode = ModeEnum.AverageFill;
    public CircleAxis circleAxis = CircleAxis.Horizontal;
    const float ONE_CIRCLE = 360f;
    public bool lookAtToPivot;
    public float offset;
    public float spacing;
    public float radius;

    public enum CircleAxis
    {
        Horizontal,
        Vertical
    }



    // Start is called before the first frame update


    // Update is called once per frame
    void Update()
    {
        Execute();
    }

    void Execute()
    {

        var finalSpacing = spacing;

        if (mode == ModeEnum.AverageFill)
            finalSpacing = ONE_CIRCLE / transform.childCount;

        bool isVertical = circleAxis == CircleAxis.Vertical;
        for (int i = 0, iMax = transform.childCount; i < iMax; i++)
        {
            var quat = Quaternion.AngleAxis(i * finalSpacing + offset, isVertical ? Vector3.forward : Vector3.up);
            var current = transform.position + quat * (isVertical ? Vector3.up : Vector3.forward) * radius;

            transform.GetChild(i).position = current;

            if (lookAtToPivot)
            {
                if (isVertical)
                {
                    transform.GetChild(i).forward = -(transform.position - transform.GetChild(i).position).normalized;

                }
                else
                {
                    transform.GetChild(i).forward = -(transform.position - transform.GetChild(i).position).normalized;
                }
            }

        }
    }
}
